package com.example.springboottest.example.equals;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class Stu {
    private String name;
    private int age;

    public Stu(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Stu stu = (Stu) o;
        return age == stu.age &&
                Objects.equals(name, stu.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    public static void main(String[] args) {
        Stu stu1 = new Stu("tom", 21);
        Stu stu2 = new Stu("tom", 21);
//
        System.out.println("stu1 == stu2\t" + (stu1 == stu2));
        System.out.println("stu1.equals(stu2)\t" + (stu1.equals(stu2)));

        System.out.println("stu1.hashCode=\t" + (Objects.hashCode(stu1)));
        System.out.println("stu2.hashCode=\t" + (Objects.hashCode(stu2)));
//
        Map<Stu, String> map = new HashMap<>();
        map.put(stu1, null);
        map.put(stu2, null);
        System.out.println("map.size()=\t" + map.size());

//        String s1 = null;
//        String s2 = null;
//        System.out.println("Objects.equals(s1,s2)\t" + Objects.equals(s1, s2));

        System.out.println("1".equals(null));
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
